package com.redis.redisdemo.controller;/**
 * @Description
 * @auther 35252
 * @create 2023-07-13 12:17
 **/

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.json.JSONUtil;
import com.redis.redisdemo.util.RedisUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName CommonController
 * @Description TODO
 * @Author 35252
 * @Date 2023/7/13 12:17
 * @Version 1.0
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/common")
@Slf4j
public class CommonController {

    @RequestMapping(value = "redis")
    public String Controller(HttpServletRequest request) {
        String contentType = request.getContentType();
        String flag="false";
        String key="one-key";
        if(contentType.contains("multipart/form-data")){
            return "对不起，该接口不支持表单形式提交";
        }else{
           Map<String,String> strMap = JSONUtil.toBean(ServletUtil.getBody(request), Map.class);
           if(strMap.containsKey("val")){
               String val=strMap.get("val");
               /**
                * 这里默认存储的key名为one-key,也可以自定义为其他的key名字
                */
               long keyTime=RedisUtil.express(key);
               if(keyTime==-2){
                   return flag;
               }else{
                  String keyVal=RedisUtil.get(key);
                  log.info("one-key:{}",keyVal);
                  boolean boo=StrUtil.equals(keyVal,val);
                  log.info("判断结果为:{}",boo);
                   return boo==true?"true":flag;
               }
           }else{
               return "对不起，改字段中未包含关键字段";
           }
        }
    }
}
